package com.danger.Activity.Setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.danger.Activity.Account.AccountActivity;
import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Presenter.SettingPresenter;
import com.danger.Presenter.SettingPresenterImpl;
import com.danger.View.CommonView;

import cn.bmob.v3.BmobUser;

/**
 * Created by danger on 2016/4/23.
 */
public class SettingFragment extends BackHandledFragment implements CommonView, View.OnClickListener {

    private View view;
    private boolean hadIntercept;
    private Toolbar setting_toolbar;
    private RelativeLayout audio_type_linear;
    private RelativeLayout audio_rate_linear;
    private RelativeLayout audio_subtitle_linear;
    private RelativeLayout clean_cache_linear;
    private RelativeLayout default_linear;
    private RelativeLayout check_update_linear;
    private RelativeLayout quit_login_linear;
    private SettingPresenter presenter;
    private SettingActivity activity;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_setting, container, false);
        initView();
        initEvent();
        return view;
    }

    private void initView() {
        activity = (SettingActivity) getActivity();
        setting_toolbar = activity.getToolbar();
        setting_toolbar.setTitle("");
        activity.getTextViewTitle().setText("设置");
        activity.setSupportActionBar(setting_toolbar);
        audio_type_linear = (RelativeLayout) view.findViewById(R.id.audio_type_linear);
        audio_rate_linear = (RelativeLayout) view.findViewById(R.id.audio_rate_linear);
        audio_subtitle_linear = (RelativeLayout) view.findViewById(R.id.audio_subtitle_linear);
        clean_cache_linear = (RelativeLayout) view.findViewById(R.id.clean_cache_linear);
        default_linear = (RelativeLayout) view.findViewById(R.id.default_linear);
        check_update_linear = (RelativeLayout) view.findViewById(R.id.check_update_linear);
        quit_login_linear = (RelativeLayout) view.findViewById(R.id.quit_login_linear);
        presenter = new SettingPresenterImpl(activity, this);
    }

    private void initEvent() {
        setting_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        audio_type_linear.setOnClickListener(this);
        audio_rate_linear.setOnClickListener(this);
        audio_subtitle_linear.setOnClickListener(this);
        clean_cache_linear.setOnClickListener(this);
        default_linear.setOnClickListener(this);
        check_update_linear.setOnClickListener(this);
        quit_login_linear.setOnClickListener(this);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showSuccess(String message, String object) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(activity, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.audio_type_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.setting_container, new AudioTypeFragment()).commit();
                break;
            case R.id.audio_rate_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.setting_container, new AudioRateFragment()).commit();
                break;
            case R.id.audio_subtitle_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.setting_container, new AudioSubtitleFragment()).commit();
                break;
            case R.id.clean_cache_linear:
                break;
            case R.id.default_linear:
                presenter.resetAsDefault();
                break;
            case R.id.check_update_linear:
                break;
            case R.id.quit_login_linear:
                BmobUser.logOut(activity);   //清除缓存用户对象
                BmobUser currentUser = BmobUser.getCurrentUser(activity); // 现在的currentUser是null了
                startActivity(new Intent(activity, AccountActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void back() {

    }
}